import Vue from 'vue'
import VueRouter from 'vue-router'
import HomePage from "@/views/HomePage.vue";
import LoginPage from "@/views/LoginPage.vue";
import MaterialSavePage from "@/views/material/MaterialSavePage.vue";
import MaterialPage from "@/views/material/MaterialPage.vue";
import ProviderPage from "@/views/provider/ProviderPage.vue";
import ProviderSavePage from "@/views/provider/ProviderSavePage.vue";

Vue.use(VueRouter)

const routes = [
  {path: '/login', component: LoginPage},
  {path: '/', component: HomePage, children:[
      {path: '/material/save', component: MaterialSavePage},
      {path: '/material/page', component: MaterialPage},
          {path: '/provider/save', component: ProviderSavePage},
          {path: '/provider/page', component: ProviderPage},
    ]}
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})



router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
    return
  }
  Vue.prototype.$http.get('/user/session')
      .then((result)=> {
          console.log('then')
        if (result.data.code == 10000) {
          next()
        } else if (result.data.code == 10007) {
          next({ path: '/login' })
        }
      })
      .catch((error)=>{
          console.log('catch')
      })
});


export default router
